package com.keldian.mobilesafe.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.TextView;

import com.keldian.mobilesafe.R;
import com.keldian.mobilesafe.dao.CommonNumberDao;
import com.keldian.mobilesafe.dao.CommonNumberDao.GroupInfo;

import java.util.ArrayList;

/**
 * Created by Administrator on 16-2-5.
 */
public class CommonNumberActivity extends Activity {

    private ExpandableListView elvList;
    private CommonNumberAdapter mAdapter;
    private ArrayList<GroupInfo> mList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_common_number);
        elvList = (ExpandableListView) findViewById(R.id.elv_list);

        mList = CommonNumberDao.getCommonNumberGroups();// 获取数据库信息

        mAdapter = new CommonNumberAdapter();
        elvList.setAdapter(mAdapter);

        createTest();

        elvList.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {

            @Override
            public boolean onChildClick(ExpandableListView parent, View v,
                                        int groupPosition, int childPosition, long id) {
                System.out.println("第" + groupPosition + "组-第" + childPosition
                        + "项被点击");

                // 跳转到拨打电话页面
                CommonNumberDao.ChildInfo childInfo = mAdapter.getChild(groupPosition,
                        childPosition);
                Intent intent = new Intent(Intent.ACTION_CALL);
                intent.setData(Uri.parse("tel:" + childInfo.number));
                try {
                    startActivity(intent);
                } catch (Exception e) {
                    e.printStackTrace();
                }

                return false;
            }
            
        });
    }

    private void createTest() {
    }

    class CommonNumberAdapter extends BaseExpandableListAdapter {

        // 获取组的数量
        @Override
        public int getGroupCount() {
            // return 8;
            return mList.size();
        }

        // 获取某个组的孩子数量
        @Override
        public int getChildrenCount(int groupPosition) {
            // return groupPosition + 1;
            return mList.get(groupPosition).children.size();
        }

        // 返回组的对象
        @Override
        public GroupInfo getGroup(int groupPosition) {
            return mList.get(groupPosition);
        }

        // 返回孩子的对象
        @Override
        public CommonNumberDao.ChildInfo getChild(int groupPosition, int childPosition) {
            return mList.get(groupPosition).children.get(childPosition);
        }

        @Override
        public long getGroupId(int groupPosition) {
            return groupPosition;
        }

        @Override
        public long getChildId(int groupPosition, int childPosition) {
            return childPosition;
        }

        // 默认就可以,不需要重写
        @Override
        public boolean hasStableIds() {
            return false;
        }

        // 获取组的布局
        @Override
        public View getGroupView(int groupPosition, boolean isExpanded,
                                 View convertView, ViewGroup parent) {
            TextView tvGroup = new TextView(getApplicationContext());
            tvGroup.setTextColor(Color.RED);
            tvGroup.setTextSize(20);
            // tvGroup.setText("       第" + groupPosition + "组");
            GroupInfo info = getGroup(groupPosition);
            tvGroup.setText("       " + info.name);
            return tvGroup;
        }

        // 获取孩子的布局
        @Override
        public View getChildView(int groupPosition, int childPosition,
                                 boolean isLastChild, View convertView, ViewGroup parent) {
            TextView tvChild = new TextView(getApplicationContext());
            tvChild.setTextColor(Color.BLACK);
            tvChild.setTextSize(18);
            // tvChild.setText("第" + groupPosition + "组" + "-第" + childPosition
            // + "项");
            CommonNumberDao.ChildInfo info = getChild(groupPosition, childPosition);
            tvChild.setText(info.name + "\n" + info.number);
            return tvChild;
        }

        // 孩子是否可以被点击
        @Override
        public boolean isChildSelectable(int groupPosition, int childPosition) {
            return true;
        }

    }
}
